//
//  YYEWithDrawViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/5.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEWithDrawViewController.h"
#import "YYERechargeTableViewCell.h"
#import "YYERechargeMoneyTableViewCell.h"
#import "YYEWithDrawTableViewCell.h"

@interface YYEWithDrawViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property(nonatomic,strong) NSString *payType;
@property (nonatomic,strong) UITextField *countTF;
@property (nonatomic,strong) UITextField *moneyTF;

@end

@implementation YYEWithDrawViewController

static NSString * withdrawCell = @"wihdrawCell";
static NSString * acountCell = @"moneyCell";
static NSString * moneyCell = @"withDrawCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"提现";
    
    self.view.backgroundColor = GeneralBackColor;
    
    [self configUI];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.payType = @"2";
        
    });
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = GeneralBackColor;
    self.tableView.tableFooterView = [self footerView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[YYERechargeTableViewCell class] forCellReuseIdentifier:withdrawCell];
    [self.tableView registerClass:[YYERechargeMoneyTableViewCell class] forCellReuseIdentifier:acountCell];
    [self.tableView registerClass:[YYEWithDrawTableViewCell class] forCellReuseIdentifier:moneyCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];

    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

- (UIView *)footerView {
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 69)];
    contentView.backgroundColor = GeneralBackColor;
    
    UIButton *withDrawBtn = [[UIButton alloc]init];
    [withDrawBtn setTitle:@"确认提现" forState:UIControlStateNormal];
    [withDrawBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    withDrawBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    withDrawBtn.layer.cornerRadius = 5;
    [withDrawBtn setBackgroundColor:GeneralBlueColor];
    [withDrawBtn addTarget:self action:@selector(withDrawClicked) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:withDrawBtn];
    
    [withDrawBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView.left).offset(30);
        make.right.equalTo(contentView.right).offset(-30);
        make.top.equalTo(contentView.top).offset(27);
        make.height.equalTo(42);
    }];
    
    return contentView;
    
}

#pragma mark -- UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
//        return 2;
        return 1;

    }else {
        return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        YYERechargeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:withdrawCell];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *names = @[@"微信支付",@"支付宝支付"];
        NSArray *imgNames = @[@"weixin",@"zhifubao"];
        
        cell.nameLabel.text = names[indexPath.row];
        cell.headerIV.image = [UIImage imageNamed:imgNames[indexPath.row]];
        cell.block = ^(NSInteger row) {
            
            if (row == 0) {
                self.payType = @"2";
            }
            else {
                self.payType = @"1";
                
            }
        };
        
        if (indexPath.row == 1) {
            cell.lineView.hidden = YES;
        }
        
        return cell;
        
    }else if (indexPath.section == 1) {
        
        YYERechargeMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:acountCell];
        
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.nameLabel.text = @"账号";
        self.countTF = cell.moneyTF;

        return cell;
        
    }else {
        
        YYEWithDrawTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:moneyCell];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.moneyTF = cell.moneyTF;
        [cell.allBtn addTarget:self action:@selector(allWithDraw) forControlEvents:UIControlEventTouchUpInside];
        
        cell.balanceLabel.text = [NSString stringWithFormat:@"可用余额 %@",self.yuE];

        return cell;
        
    }
    
}
- (void)allWithDraw
{
    self.moneyTF.text = self.yuE;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 138.5
    if (indexPath.section == 2) {
        return 138.5;
    }else {
        return 50;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    contentView.backgroundColor = GeneralBackColor;
    
    return contentView;
    
}

// 确认提现
- (void)withDrawClicked {
    
    if (_countTF.text.length == 0) {
        [self.view makeToast:@"请输入账号"];
        return;
    }
    
    if (_moneyTF.text.length == 0) {
        [self.view makeToast:@"请输入金额"];
        return;
    }
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setValue:[LoginModel shareLogin].uid forKey:@"uid"];
    [paramDic setValue:[LoginModel shareLogin].token forKey:@"token"];
    [paramDic setValue:[LoginModel shareLogin].username forKey:@"username"];
    
    
    [paramDic setValue:self.payType forKey:@"type"];
    [paramDic setValue:_moneyTF.text forKey:@"money"];
    [paramDic setValue:_countTF.text forKey:@"account"];

    
    [AFNHelper post:Drawings parameters:paramDic success:^(id responseObject) {
        
        if ([responseObject[@"status"] isEqual:@1]) {

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"1-3个工作日提现成功。" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                //充值或提现成功通知事件
                [[NSNotificationCenter defaultCenter] postNotificationName:@"kchargeOrDepositSuccessNotification" object:nil];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }];
            //    [okAction setValue:[UIColor colorWithHexString:@"#D0021B"] forKey:@"_titleTextColor"];
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
        }
        else {
            [self.view makeToast:responseObject[@"message"]];
        }
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
